
//
//  YWPushManager.m
//  maintain
//
//  Created by 郑想 on 15/11/11.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "MJExtension.h"
#import "YWConst.h"
#import "YWLoginTool.h"
#import "YWMacro.h"
#import "YWMission.h"
#import "YWMissionManager.h"
#import "YWHandler.h"
#import "YWNotifyManager.h"
#import "YWPush.h"
#import "YWPushManager.h"
#import "YWWorkReminderManager.h"
#import "YWDepartment.h"
#import "YWDownloadMetaDataManager.h"
#import "YWNoticeManager.h"
#import "YWTempMissionManager.h"
#import "YWReplyManager.h"
#import "YWKeepWatchMissionManager.h"
#import "YWInspectionMissionManager.h"
#import "UMessage.h"
#import "YWMaterialManager.h"
#import "YWDetectAndUpkeepManager.h"
#import "YWReportTroubleManager.h"
#import "YWReportTroubleModel.h"
#import <UserNotifications/UserNotifications.h>

@interface YWPushManager ()
//@property (nonatomic, strong) AVInstallation* currentInstallation;
@end

@implementation YWPushManager
YWSingletonM(Manager)
//    - (AVInstallation*)currentInstallation
//{
//    if (!_currentInstallation) {
//        _currentInstallation = [AVInstallation currentInstallation];
//    }
//    return _currentInstallation;
//}

- (NSString*)deviceToken
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KAppDeviceToken];
}

- (void)setDeviceToken:(NSString*)deviceToken
{
    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:KAppDeviceToken];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)registerRemoteNotification
{
    if (iOS10) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = (id<UNUserNotificationCenterDelegate>)[UIApplication sharedApplication];
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                //点击允许
                //这里可以添加一些自己的逻辑
            } else {
                //点击不允许
                //这里可以添加一些自己的逻辑
            }
            if (!error) {
                YWLog(@"iOS10 registerRemoteNotification succeeded!");
            }
        }];
    }
//    else{
//        UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
//        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
//        [[UIApplication sharedApplication] registerForRemoteNotifications];
//    }
}

- (BOOL)openNotifyAlert
{
    //    if (iOS8) {
    UIUserNotificationType types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
    return (types & UIUserNotificationTypeAlert) == UIUserNotificationTypeAlert;
    //    }
    //    else {
    //        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    //        return (types & UIRemoteNotificationTypeAlert) == UIRemoteNotificationTypeAlert;
    //    }
}

//- (void)deleteSubscribeChannel:(AVBooleanResultBlock)block;
//{
//    NSArray* subscribedChannels = [self.currentInstallation.channels copy];
//    [self.currentInstallation removeObjectsInArray:subscribedChannels forKey:@"channels"];
//    [self.currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError* error) {
//        if (!succeeded) {
//            YWLog(@"%@", error.description);
//        }
//        if (block) {
//            block(succeeded, error);
//        }
//    }];
//}
//
//- (void)subscribeChannel:(YWLoginUser*)loginUser
//{
//    [self deleteSubscribeChannel:^(BOOL succeeded, NSError* error) {
//        NSString* companyAccountChannels = [NSString stringWithFormat:@"O%@", loginUser.companyAccount];
//        [self.currentInstallation addUniqueObject:companyAccountChannels forKey:@"channels"];
//        NSString* departmentChannels = nil;
//        for (YWDepartmentJson* departmentJson in loginUser.departments) {
//            departmentChannels = [NSString stringWithFormat:@"D%ld", (long)departmentJson.departmentId];
//            [self.currentInstallation addUniqueObject:departmentChannels forKey:@"channels"];
//        }
//
//        [self.currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError* error) {
//            if (!succeeded) {
//                YWLog(@"%@", error.description);
//            }
//        }];
//
//    }];
//}
//
//- (void)deleteAvosEwinUser{
//    AVQuery* query = [AVQuery queryWithClassName:@"AVEwinUser"];
//    [query whereKey:@"uniqueId" equalTo:@([YWLoginTool loginUser].uniqueId)];
//    [query findObjectsInBackgroundWithBlock:^(NSArray* objects, NSError* error) {
//        if (!error) {
//            if (objects.count) {
//                [query deleteAllInBackgroundWithBlock:^(BOOL succeeded, NSError* error) {
//                    if (succeeded) {
//                        YWLog(@"deleteEwinUser succeeded");
//                    }else{
//                        YWLog(@"deleteEwinUser Error: %@ %@", error, [error userInfo]);
//                    }
//                }];
//            }
//            else {
//                YWLog(@"NoObjects");
//            }
//        }
//        else {
//            YWLog(@"saveEwinUser Error: %@ %@", error, [error userInfo]);
//        }
//    }];
//}
//
//- (void)avosSetDeviceToken:(NSData*)deviceToken
//{
//    [self.currentInstallation setDeviceTokenFromData:deviceToken];
//    [self.currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError* error) {
//        if (!succeeded) {
//            YWLog(@"%@", error.userInfo);
//        }
//    }];
//}
//
//- (void)avosClearBadge
//{
//    NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber;
//    if (num != 0) {
//        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
//    }
//    [self.currentInstallation setBadge:0];
//    [self.currentInstallation saveEventually];
//    [[UIApplication sharedApplication] cancelAllLocalNotifications];
//}
//
//- (void)setEwinUser:(AVObject*)ewinUser
//{
//    [ewinUser setObject:@([YWLoginTool loginUser].uniqueId) forKey:@"uniqueId"];
//    [ewinUser setObject:@"ios" forKey:@"deviceType"];
//    if ([YWLoginTool isLogin]) {
//        [ewinUser setObject:[self deviceToken] forKey:@"deviceToken"];
//    }
//    else {
//        [ewinUser setObject:@"" forKey:@"deviceToken"];
//    }
//
//    [ewinUser setObject:@"" forKey:@"installationId"];
//    [ewinUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError* error) {
//        if (!succeeded) {
//            YWLog(@"saveEwinUser %@", error.userInfo);
//        }
//        else {
//            [ewinUser saveInBackgroundWithBlock:nil];
//        }
//    }];
//}
//
//- (void)saveEwinUser
//{
//    AVQuery* query = [AVQuery queryWithClassName:@"AVEwinUser"];
//    [query whereKey:@"uniqueId" equalTo:@([YWLoginTool loginUser].uniqueId)];
//    [query findObjectsInBackgroundWithBlock:^(NSArray* objects, NSError* error) {
//        if (!error) {
//            if (objects.count) {
//                [query deleteAllInBackgroundWithBlock:^(BOOL succeeded, NSError* error) {
//                    if (succeeded) {
//                        AVObject* ewinUser = [AVObject objectWithClassName:@"AVEwinUser"];
//                        [self setEwinUser:ewinUser];
//                    }
//                }];
//            }
//            else {
//                AVObject* ewinUser = [AVObject objectWithClassName:@"AVEwinUser"];
//                [self setEwinUser:ewinUser];
//            }
//        }
//        else {
//            YWLog(@"saveEwinUser Error: %@ %@", error, [error userInfo]);
//        }
//    }];
//}

//- (NSDictionary*)dicWithMission:(YWMission*)mission action:(NSString*)action
//{
//    NSDictionary* dic = nil;
//    if (mission) {
//        if ([action isEqualToString:KPush_Mission]) {
//            YWPushMission* pushMission = [[YWPushMission alloc] init];
//            pushMission.missionId = mission.missionId;
//            pushMission.maintenanceTypeId = mission.maintenanceTypeId;
//            pushMission.missionStatus = mission.status;
//            pushMission.completedQuantity = mission.completedQuantity;
//            NSString* contentJson = [pushMission mj_JSONString];
//            dic = @{
//                @"action" : action,
//                @"content" : contentJson
//            };
//        }
//        else if ([action isEqualToString:KPush_Trouble]) {
//            YWPushTrouble* pushTrouble = [[YWPushTrouble alloc] init];
//            pushTrouble.troubleId = mission.missionId;
//            pushTrouble.maintenanceTypeId = mission.maintenanceTypeId;
//            pushTrouble.status = mission.status;
//            NSString* contentJson = [pushTrouble mj_JSONString];
//            dic = @{
//                @"action" : action,
//                @"content" : contentJson
//            };
//        }
//        else if ([action isEqualToString:KPush_Assign]) {
//            YWPushAssign* pushAssign = [[YWPushAssign alloc] init];
//            pushAssign.troubleId = mission.missionId;
//            NSString* contentJson = [pushAssign mj_JSONString];
//            dic = @{
//                @"badge" : @"Increment",
//                @"alert" : @"您有一条新故障信息,请及时处理",
//                @"sound" : @"cheering.caf",
//                @"action" : action,
//                @"content" : contentJson
//            };
//        }
//        else if ([action isEqualToString:KPush_Report]) {
//            dic = @{
//                @"badge" : @"Increment",
//                @"alert" : @"您有一条新故障信息,请及时处理",
//                @"sound" : @"cheering.caf",
//                @"action" : action
//            };
//        }
//    }
//
//    return dic;
//}

//- (void)pushToUserWithUniqueId:(NSInteger)uniqueId mission:(YWMission*)mission action:(NSString*)action
//{
//    if (uniqueId <= 0) {
//        return;
//    }
//    AVQuery* ewinUserQuery = [AVQuery queryWithClassName:@"AVEwinUser"];
//    [ewinUserQuery whereKey:@"uniqueId" equalTo:@(uniqueId)];
//
//    [ewinUserQuery findObjectsInBackgroundWithBlock:^(NSArray* objects, NSError* error) {
//
//        if (!error) {
//            [objects enumerateObjectsUsingBlock:^(AVObject* _Nonnull ewinUser, NSUInteger idx, BOOL* _Nonnull stop) {
//                AVQuery* installationQuery = [AVInstallation query];
//                BOOL isIos = [[ewinUser objectForKey:@"deviceType"] isEqualToString:@"ios"];
//                if (isIos) {
//                    YWLog(@"----------%@", [ewinUser objectForKey:@"deviceToken"]);
//                    [installationQuery whereKey:@"deviceToken" equalTo:[ewinUser objectForKey:@"deviceToken"] ?: @""];
//                }
//                else {
//                    [installationQuery whereKey:@"installationId" equalTo:[ewinUser objectForKey:@"installationId"] ?: @""];
//                }
//
//#ifdef Production
//                [AVPush setProductionMode:YES];
//#else
//                [AVPush setProductionMode:NO];
//#endif
//                AVPush* push = [[AVPush alloc] init];
//                [push setQuery:installationQuery];
//
//                NSDictionary* data = [self dicWithMission:mission action:action];
//                if (data) {
//                    [push setData:data];
//                    [push sendPushInBackground];
//                }
//            }];
//        }
//        else {
//            YWLog(@"saveEwinUser Error: %@ %@", error, [error userInfo]);
//        }
//    }];
//}
- (void)setUpUMengAliasWithUser:(YWLoginUser *)loginUser{
    NSString *alias = [NSString stringWithFormat:@"uniqueId_%ld",(long)loginUser.uniqueId];
    [UMessage removeAlias:alias type:@"EWIN" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        if(responseObject){
            YWLog(@"删除umengAlias成功");
        }else{
            YWLog(@"删除umengAlias失败---%@",error.localizedDescription);
        }
        [UMessage setAlias:alias type:@"EWIN" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
            if(responseObject){
                YWLog(@"设置umengAlias成功");
            }else{
                YWLog(@"设置umengAlias失败---%@",error.localizedDescription);
            }
        }];
    }];
}

- (void)setUpUMengTagsWithUser:(YWLoginUser *)loginUser{
    [UMessage removeAllTags:^(id  _Nonnull responseObject, NSInteger remain, NSError * _Nonnull error) {
        if(responseObject){
            YWLog(@"删除所有umengTags成功");
        }else{
            YWLog(@"删除所有umengTags失败---%@",error.localizedDescription);
        }
        NSMutableArray *tags = [NSMutableArray array];
        NSString* companyAccount = [NSString stringWithFormat:@"Organization_%@", loginUser.companyAccount];
        [tags addObject:companyAccount];
        for (YWDepartmentJson* departmentJson in loginUser.departments) {
            NSString *departmentChannels = [NSString stringWithFormat:@"Department_%ld", (long)departmentJson.departmentId];
            [tags addObject:departmentChannels];
        }
        if (tags.count) {
            [UMessage addTag:tags response:^(id  _Nonnull responseObject, NSInteger remain, NSError * _Nonnull error) {
                if(responseObject){
                    YWLog(@"添加umengTag成功");
                }else{
                    YWLog(@"添加umengTag失败---%@",error.localizedDescription);
                }
            }];
        }
    }];
}

- (void)removeAllUMengAliasAndTags{
    [UMessage removeAllTags:^(id  _Nonnull responseObject, NSInteger remain, NSError * _Nonnull error) {
        if(responseObject){
            YWLog(@"退出登录删除所有umengTags成功");
        }else{
            YWLog(@"退出登录删除所有umengTags失败---%@",error.localizedDescription);
        }
    }];
    YWLoginUser *currentUser = [YWLoginTool loginUser];
    NSString *alias = [NSString stringWithFormat:@"uniqueId_%ld",(long)currentUser.uniqueId];
    [UMessage removeAlias:alias type:@"EWIN" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        if(responseObject){
            YWLog(@"退出登录删除umengAlias成功");
        }else{
            YWLog(@"退出登录删除umengAlias失败---%@",error.localizedDescription);
        }
    }];
}

- (void)handleRemoteNoti:(NSDictionary*)userInfo
{
    NSString* action = [userInfo objectForKey:@"action"];
    NSString* content = [userInfo objectForKey:@"content"];
    if ([action isEqualToString:KPush_Mission]) {
        YWPushMission* pushMission = [YWPushMission mj_objectWithKeyValues:content];
        NSDictionary* dic = @{
            @"maintenanceType" : @(pushMission.maintenanceTypeId),
            @"status" : @(pushMission.missionStatus),
            @"missionId" : @(pushMission.missionId),
            @"completedQuantity" : @(pushMission.completedQuantity)
        };
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:pushMission];
        [[YWNotifyManager shareManager] postMissionProgressNotify:dic];
        
    }else if ([action isEqualToString:KPush_Trouble]) {
        YWPushTrouble* pushTrouble = [YWPushTrouble mj_objectWithKeyValues:content];
        NSDictionary* dic = @{
            @"maintenanceType" : @(YWMaintenanceTypeTrouble),
            @"status" : @(YWTroubleMissionStatusFinish),
            @"missionId" : @(pushTrouble.troubleId)
        };
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:pushTrouble];
        [[YWNotifyManager shareManager] postMissionProgressNotify:dic];
        
    }else if ([action isEqualToString:KPush_Assign]) {
        YWPushAssign* pushAssign = [YWPushAssign mj_objectWithKeyValues:content];
        
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:pushAssign];
        
    }else if ([action isEqualToString:KPush_Report]) {
        //报障
        YWPushReport *pushReport = [YWPushReport mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:pushReport];
//        YWReportTroubleModel * reportTrouble = [[YWReportTroubleManager shareManager] getOneReportTroubleWithTroubleId:pushReport.troubleId];
//        if (!reportTrouble) {
//            [YWLoginTool saveReportTroubleNoReadCount];
//            [[NSNotificationCenter defaultCenter] postNotificationName:KPostReportTroubleNoReadCountNotification object:nil];
//        }
        [YWLoginTool saveReportTroubleNoReadCount];
        [[NSNotificationCenter defaultCenter] postNotificationName:KPostReportTroubleNoReadCountNotification object:nil];
        
    }else if ([action isEqualToString:KPush_CommonMission]) {
        YWPushCommonMission *pushCommonMission = [YWPushCommonMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:pushCommonMission];
        
        YWMaintenanceMission *mission = [YWDetectAndUpkeepManager maintenanceMissionWithMissionId:pushCommonMission.missionId];
        if (!mission) {
            //检测保养任务未读数
            [YWLoginTool saveMissionNoRedCountWithMaintenanceType:pushCommonMission.maintenanceTypeId];
            if (pushCommonMission.maintenanceTypeId == YWMaintenanceTypeDetection) {
                [[YWNotifyManager shareManager] postAssignDetectMissionNotify];
            }else{
                [[YWNotifyManager shareManager] postAssignUpKeepMissionNotify];
            }
        }
    }else if ([action isEqualToString:KPush_WorkTask]) {
        //临时任务推送
        YWPushWorkTask* pushWorkTask = [YWPushWorkTask mj_objectWithKeyValues:content];
        if (![[YWTempMissionManager shareManager] existTempMissionWithTempMissionId:pushWorkTask.workTaskId]) {
            [YWLoginTool saveTempMissionNoRedCount];
            //后台会推送给创建者和接受者，可能发一条发送者又是接受者红色数字为2
            [[YWNotifyManager shareManager] postPushTempMissionNotify:pushWorkTask];
        }
        
    }else if ([action isEqualToString:KPush_Reply]){
        //评论的推送
        YWPushReply *pushReply = [YWPushReply mj_objectWithKeyValues:content];
        if (pushReply.reply) {
            pushReply.reply.relationId = pushReply.relationId;
            pushReply.reply.relationType = pushReply.relationType;
            [[YWReplyManager shareManager] updateReplyWithReplyArray:@[pushReply.reply]];
            if (pushReply.reply.replier.uniqueId != [YWLoginTool loginUser].uniqueId) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:KPostRefreshUnreadReplyNotification object:nil];
                });
            }
        }
    }else if ([action isEqualToString:KPush_WorkNotice]) {
        //通知
        YWPushWorkNotice* pushWorkNotice = [YWPushWorkNotice mj_objectWithKeyValues:content];
        if (![[YWNoticeManager shareManager] existNoticeWithNoticeId:pushWorkNotice.workNoticeId]) {
            [YWLoginTool saveNoticeNoRedCount];
            //后台会推送给创建者和接受者，可能发一条发送者又是接受者红色数字为2
            [[YWNotifyManager shareManager] postPushNoticeNotify:pushWorkNotice];
        }
    }else if ([action isEqualToString:KPush_CancelMission]) {
        YWPushCancelMission* cancelMission = [YWPushCancelMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:cancelMission];
    }else if ([action isEqualToString:KPush_CancelPostTrouble]) {
        //取消报障
        YWPushCancelPostTrouble* cancelPostTrouble = [YWPushCancelPostTrouble mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:cancelPostTrouble];
    }else if ([action isEqualToString:KPush_CancelTroubleMission]){
        //撤回维修任务
        YWPushCancelTroubleMission* cancelTrouble = [YWPushCancelTroubleMission mj_objectWithKeyValues:content];
        [[NSNotificationCenter defaultCenter] postNotificationName:KPostReportTroubleNoReadCountNotification object:nil];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:cancelTrouble];
    }else if ([action isEqualToString:KPush_RejectTroubleMission]){
        //退回
        YWPushRejectTroubleMission *rejectTroubleMission = [YWPushRejectTroubleMission mj_objectWithKeyValues:content];
        [[NSNotificationCenter defaultCenter] postNotificationName:KPostReportTroubleNoReadCountNotification object:nil];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:rejectTroubleMission];
    }else if ([action isEqualToString:KPush_JsonRefresh]){
        //json静态数据有更新的推送
        YWPushJsonRefresh *jsonRefresh = [YWPushJsonRefresh mj_objectWithKeyValues:content];
        [[YWDownloadMetaDataManager shareManager] downloadDatasWithType:jsonRefresh.type];
    }else if ([action isEqualToString:KPush_ModifyMission]){
        //修改检测、保养任务的推送
        YWPushMissionModify *missionModify = [YWPushMissionModify mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:missionModify];
        
    }else if ([action isEqualToString:KPush_KeepWatchMission]){
        //新的巡更任务推送
        YWPushKeepWatchMission *keepWatchMission = [YWPushKeepWatchMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:keepWatchMission];
        YWKeepWatchMission *keepwatchMission = [[YWKeepWatchMissionManager shareManager] keepWatchMissionWithMissionId:keepWatchMission.keepWatchMissionId];
        if (keepwatchMission==nil || keepwatchMission.status!=0) {
            //巡更任务未读数
            [YWLoginTool saveMissionNoRedCountWithMaintenanceType:YWMaintenanceTypeKeepWatch];
            [[YWNotifyManager shareManager] postAssignKeepWatchMissionNotify];
        }
        
    }else if ([action isEqualToString:KPush_KeepWatchMissionProgress]){
        //巡更进度推送（不显示出来，只请求同步记录）
        YWPushKeepWatchMissionProgress *keepWatchMissionProgress = [YWPushKeepWatchMissionProgress mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:keepWatchMissionProgress];
    }else if ([action isEqualToString:KPush_FinishKeepWatchMission]){
        //完成一次巡更的推送-----(铃铛显示)
        YWPushFinishKeepWatchMission *finishKeepWatchMission = [YWPushFinishKeepWatchMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:finishKeepWatchMission];
        
    }else if ([action isEqualToString:KPush_ModifyKeepWatchMission]){
        //修改巡更任务
        YWPushModifyKeepWatchMission *modifyKeepWatchMission = [YWPushModifyKeepWatchMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:modifyKeepWatchMission];
        
    }else if ([action isEqualToString:KPush_CancelKeepWatchMission]){
        //取消巡更任务
        YWPushCancelKeepWatchMission *pushCancelKeepWatchMission = [YWPushCancelKeepWatchMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:pushCancelKeepWatchMission];
        
    }else if ([action isEqualToString:KPush_InspectionMission]){
        //收到新的巡查任务
        YWPushInspectionMission *pushInspectionMission = [YWPushInspectionMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:pushInspectionMission];
        YWInspectionMissionModel *inspectionMission = [[YWInspectionMissionManager shareManager] inspectionMissionWithMissionId:pushInspectionMission.inspectionMissionId];
        if (inspectionMission==nil || inspectionMission.status!=0) {
            //巡查任务未读数
            [YWLoginTool saveMissionNoRedCountWithMaintenanceType:YWMaintenanceTypePatrol];
            [[YWNotifyManager shareManager] postAssignInspectMissionNotify];
        }
        
    }else if ([action isEqualToString:KPush_CancelInspectionMission]){
        //取消巡查任务
        YWPushCancelInspectionMission *cancelInspectionMission = [YWPushCancelInspectionMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:cancelInspectionMission];
        
    }else if ([action isEqualToString:KPush_ModifyInspectionMission]){
        //修改巡查任务
        YWPushModifyInspectionMission *modifyInspectionMission = [YWPushModifyInspectionMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:modifyInspectionMission];
        
    }else if ([action isEqualToString:KPush_InspectionMissionProgress]){
        //巡查任务有新的进度（不显示出来，只请求同步记录）
        YWPushInspectionMissionProgress *inspectionMissionProgress = [YWPushInspectionMissionProgress mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:inspectionMissionProgress];
    }else if ([action isEqualToString:KPush_FinishInspectionMission]){
        //完成巡查任务-----(铃铛显示)
        YWPushFinishInspectionMission *finishInspectionMission = [YWPushFinishInspectionMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:finishInspectionMission];
    }else if ([action isEqualToString:KPush_DeleteWorkNotice]){
        //删除通知
        KPushDeleteWorkNotice *workNotice = [KPushDeleteWorkNotice mj_objectWithKeyValues:content];
        if (workNotice.workNoticeId) {
            [[YWNoticeManager shareManager] deleteNoticeWithNoticeId:workNotice.workNoticeId];
        }
    }else if ([action isEqualToString:KPush_ModifyWorkNotice]){
        //修改通知
        KPushModifyWorkNotice *workNotice = [KPushModifyWorkNotice mj_objectWithKeyValues:content];
        YWNotice *notice = [[YWNoticeManager shareManager] noticeWithNoticeId:workNotice.workNoticeId];
        if (notice && notice.fromUser.uniqueId!=[YWLoginTool loginUser].uniqueId) {
            [YWLoginTool saveNoticeNoRedCount];
            [[YWNotifyManager shareManager] postPushNoticeNotify:nil];
        }
    }else if ([action isEqualToString:KPush_DeleteWorkTask]){
        //删除临时任务
        KPushDeleteWorkTask *workTask = [KPushDeleteWorkTask mj_objectWithKeyValues:content];
        if (workTask.workTaskId) {
            [[YWTempMissionManager shareManager] deleteTempMissionWithTempMissionId:workTask.workTaskId];
        }
    }else if ([action isEqualToString:KPush_ModifyWorkTask]){
        //修改临时任务
        KPushModifyWorkTask *workTask = [KPushModifyWorkTask mj_objectWithKeyValues:content];
        YWTempMission *tempMission = [[YWTempMissionManager shareManager] tempMissionWithTempMissionId:workTask.workTaskId];
        if (tempMission && tempMission.fromUser.uniqueId!=[YWLoginTool loginUser].uniqueId) {
            [YWLoginTool saveTempMissionNoRedCount];
            [[YWNotifyManager shareManager] postPushTempMissionNotify:nil];        }
    }else if ([action isEqualToString:KPush_EXIT]){
        //退出登录的推送
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:KPushLogOutNotification object:nil  userInfo:userInfo];
        });
    }else if ([action isEqualToString:KPush_DeleteReply]){
        KPushDeleteReply *pushDeleteReply = [KPushDeleteReply mj_objectWithKeyValues:content];
        [[YWReplyManager shareManager] deleteReplyWithReplyId:pushDeleteReply.replyId];
    }else if ([action isEqualToString:KPush_SystemKeepWatchMission]){
        //系统生成巡更记录
        KPushSystemKeepWatchMission *systemkeepWatch = [KPushSystemKeepWatchMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:systemkeepWatch];
        
    }else if ([action isEqualToString:KPush_SystemInspectionMission]){
        //系统生成巡查记录
        KPushSystemInspectionMission *systeminspection = [KPushSystemInspectionMission mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:systeminspection];
    }else if ([action isEqualToString:KPush_MATERIAL_APPLY]){
        //物料申请推送
        KPushMaterialApply *materialApply = [KPushMaterialApply mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:materialApply];
        [YWLoginTool saveMaterialNoReadCount];
        [[NSNotificationCenter defaultCenter] postNotificationName:KPostMaterialNoReadCountNotification object:nil];
        
    }else if ([action isEqualToString:KPush_MATERIAL_APPLY_UPDATE]){
        //物料申请抄送人
        KPushMaterialApplyUpdate *materialUpdate = [KPushMaterialApplyUpdate mj_objectWithKeyValues:content];
        if (materialUpdate.creator.uniqueId!=[YWLoginTool loginUser].uniqueId) {
            [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:materialUpdate];
            [YWLoginTool saveMaterialNoReadCount];
            [[NSNotificationCenter defaultCenter] postNotificationName:KPostMaterialNoReadCountNotification object:nil];
        }
        
    }else if ([action isEqualToString:KPush_MATERIAL_CANCEL]){
        //物料申请撤回
        KPushMaterialCancel *materialCancel = [KPushMaterialCancel mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:materialCancel];
        
    }else if ([action isEqualToString:KPush_MATERIAL_REJECT]){
        //物料申请驳回
        KPushMaterialReject *materialReject = [KPushMaterialReject mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:materialReject];
        
    }else if ([action isEqualToString:KPush_MATERIAL_MATERIAL_STOCK_OUT]){
        //物料领用
        KPushMaterialStockOut *materialStockOut = [KPushMaterialStockOut mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:materialStockOut];
        
    }else if ([action isEqualToString:KPush_MATERIAL_MATERIAL_RETURN]){
        //物料归还
        KPushMaterialReturn *materialReturn = [KPushMaterialReturn mj_objectWithKeyValues:content];
        [[YWWorkReminderManager shareManager] saveReminderWithPushMessage:materialReturn];
    }else if ([action isEqualToString:KPush_MATERIAL_APPLY_AT_CANCEL]){
        KPushMaterialApplyAtCancel *materialApplyAtCancel = [KPushMaterialApplyAtCancel mj_objectWithKeyValues:content];
        [[YWMaterialManager shareManager] deleteMaterialApplyWithApplyId:materialApplyAtCancel.applyId];
        
    }else if ([action isEqualToString:KPush_MATERIAL_APPLY_AT_CANCEL]){
        KPushMaterialStockOutAtCancel *stockOutAtCancel = [KPushMaterialStockOutAtCancel mj_objectWithKeyValues:content];
        [[YWMaterialManager shareManager] deleteMaterialStockOutWithStockOutId:stockOutAtCancel.stockOutId];
    }else if ([action isEqualToString:KPush_MATERIAL_APPLY_AT_CANCEL]){
        KPushMaterialReturnAtCancel *materialReturnAtCancel = [KPushMaterialReturnAtCancel mj_objectWithKeyValues:content];
        [[YWMaterialManager shareManager] deleteMaterialReturnWithReturnId:materialReturnAtCancel.returnId];
    }else if ([action isEqualToString:KPush_MonitorAlarm]){
        [YWLoginTool saveMonitorAlarmNoRedCount];
        [[NSNotificationCenter defaultCenter] postNotificationName:KPostMonitorAlarmUnReadCountNotification object:nil];
    }
    
    //[self avosClearBadge];
}

@end
